package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiU_against;
import cn.com.api.entity.ApiU_circle;
import cn.com.api.entity.ApiU_circle_member;
import cn.com.api.entity.ApiU_message;
import cn.com.api.entity.ApiU_user_friend_group;
import cn.com.api.entity.ApiU_user_friend_group_detail;
import cn.com.api.mapper.ApiT_memberMapper;
import cn.com.api.mapper.ApiU_againstMapper;
import cn.com.api.mapper.ApiU_circleMapper;
import cn.com.api.mapper.ApiU_circle_memberMapper;
import cn.com.api.mapper.ApiU_messageMapper;
import cn.com.api.mapper.ApiU_user_friend_groupMapper;
import cn.com.api.mapper.ApiU_user_friend_group_detailMapper;
import cn.com.api.service.ApiU_circleService;
import cn.com.api.service.impl.base.U_circleServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.PageInfo;
import cn.com.util.QRCodeUtils;
import cn.com.util.Util;
import cn.com.util.Utils;

/**
 * 
 **/
@Service
public class ApiU_circleServiceImpl extends U_circleServiceImplBase implements ApiU_circleService {
	@Autowired
	private ApiU_circleMapper apiU_circleMapper;
	@Autowired
	private ApiU_againstMapper apiU_againstMapper;
	@Autowired
	private ApiU_circle_memberMapper apiU_circle_memberMapper;
	@Autowired
	private ApiU_user_friend_group_detailMapper apiU_user_friend_group_detailMapper;
	@Autowired
	private ApiU_messageMapper apiU_messageMapper;
	@Autowired
	private ApiU_user_friend_groupMapper apiU_user_friend_groupMapper;
	@Autowired
	private ApiT_memberMapper apiT_memberMapper;

	// 新增对象后返回记录ID
	public int U_circleInsertion(ApiU_circle u_circle) {
		return this.apiU_circleMapper.U_circleInsertion(u_circle);
	}

	public List<ApiU_circle> findCirclesListPage(Map paramMap, PageInfo pageInfo) {
		return this.apiU_circleMapper.findCirclesListPage(paramMap, pageInfo);
	}

	// 获取收藏的圈子列表
	public List<ApiU_circle> findFavoriteCircleList(String user_id, Integer type, PageInfo page) {
		return this.apiU_circleMapper.findCircleListByFavoriteTypeListPage(user_id, type, page);
	}

	// 获取关注的圈子列表
	public List<ApiU_circle> findFollowCircleList(String user_id, Integer type, PageInfo page) {
		List<ApiU_circle> circle_List = this.apiU_circleMapper.findCircleListByFollowTypeListPage(user_id, type, page);
		for (ApiU_circle circle : circle_List) {
			Integer memberCount = this.apiU_circle_memberMapper.getcircle_membercount(circle.getId().toString());
			circle.setMembercount(memberCount);
		}
		return circle_List;
	}

	public List<ApiU_circle> getRecommendcircleListPage(String user_id, PageInfo pageInfo) {
		return this.apiU_circleMapper.getRecommendcircleListPage(user_id, pageInfo);
	}

	public List<ApiU_circle> getNearbycircleListPage(Map paramMap, PageInfo pageInfo) {
		return this.apiU_circleMapper.getNearbycircleListPage(paramMap, pageInfo);
	}

	// 获取我的关注、创建、加入的圈子列表
	public List<ApiU_circle> getmycircles(String user_id, String type, PageInfo pageInfo) {
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("followuser_id", user_id);// 用于判断是否关注
		if ("2".equals(type)) {// 关注
			paramMap.put("follow_user_id", user_id);
		} else {// 加入和创建的
			paramMap.put("user_id", user_id);
			paramMap.put("is_admin", type);
		}
		return this.apiU_circleMapper.findCirclesListPage(paramMap, pageInfo);
	}

	public ApiU_circle findCircleInfo(Integer circleid) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", circleid);// 用于判断是否关注
		return this.apiU_circleMapper.findCircleInfoByMap(paramMap);
	}

	public List<ApiU_against> getcircleagainstlist(String circleid, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("conn_id", circleid);
		map.put("type", 2);
		return apiU_againstMapper.getAgainstListPage(map, pageInfo);
	}

	@Transactional
	public Map<String, Object> delCircle(String circle_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		ApiU_circle apiU_circle = apiU_circleMapper.findU_circleById(Integer.valueOf(circle_id));
		apiU_circle.setIs_delete(1);
		apiU_circleMapper.updateU_circle(apiU_circle);
		// 将该圈子从所有的圈成员消息组中删除
		apiU_user_friend_group_detailMapper.delcircle(circle_id);
		// 给圈子成员发消息说明圈子解散了。
		map.put("circle_id", apiU_circle.getId());
		map.put("status", 1);
		PageInfo pageInfo = new PageInfo();
		pageInfo.setShowCount(100000);
		List<ApiU_circle_member> list = apiU_circle_memberMapper.getcirclemembersListPage(map, pageInfo);
		List<ApiU_message> messagelist = new ArrayList<ApiU_message>();
		for (ApiU_circle_member apiU_circle_member : list) {
			// 圈子成员插入系统信息
			ApiU_message message = new ApiU_message();
			message.setAdd_time(DateUtil.nowTimeToString());
			message.setConn_id(Integer.valueOf(apiU_circle_member.getUser_id()));
			message.setTitle("圈子解散");
			message.setType(8);
			message.setUser_id(apiU_circle_member.getUser_id());
			message.setIs_read(0);
			message.setContent("圈子" + apiU_circle.getCircle_name() + " 已解散");
			messagelist.add(message);

			// 将圈子从消息组中删除
			ApiU_user_friend_group_detail detail = apiU_user_friend_group_detailMapper
					.findFriendGroupDetailByFriend(apiU_circle_member.getUser_id(), circle_id, 1);
			if (detail != null) {
				apiU_user_friend_group_detailMapper.deleteU_user_friend_group_detail(detail.getId());
			}
		}
		apiU_messageMapper.insertmessageList(messagelist);
		map.clear();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作成功");
		return map;
	}

	// 创建圈子
	@Transactional
	public Map<String, Object> createCircle(String user_id, String admin_nick, String circle_memberids,
			ApiU_circle u_circle) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		PageInfo pageInfo = new PageInfo();
		map.put("isexist_circlename", u_circle.getCircle_name());
		map.put("followuser_id", user_id);
		List<ApiU_circle> list = apiU_circleMapper.findCirclesListPage(map, pageInfo);
		map.clear();
		if (list != null && list.size() > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_EXIST);
			map.put(Utils.MESSAGE, "圈子已存在！");
		} else {
			u_circle.setCreate_uid(user_id);
			u_circle.setCreate_time(DateUtil.nowTimeToString());
			apiU_circleMapper.U_circleInsertion(u_circle);
			// 生成圈子的二维码
			String qrCodeInfo = QRCodeUtils.baseUrl + "/circle/sq-ziliao.html?code=2:" + u_circle.getId();
			String filePathNew = QRCodeUtils.createQrCode(qrCodeInfo, 300);
			u_circle.setCircle_no(filePathNew);
			apiU_circleMapper.updateU_circle(u_circle);
			// 创建圈子时，在圈子成员表中加一条记录，群主对本圈的昵称
			String nowstr = DateUtil.nowTimeToString();
			ApiU_circle_member admin = new ApiU_circle_member();
			admin.setAdd_time(nowstr);
			admin.setCircle_id(u_circle.getId() + "");
			admin.setIs_admin(1);
			admin.setIs_deal(1);
			admin.setStatus(1);
			admin.setUpdate_time(nowstr);
			admin.setUser_id(user_id);
			admin.setNick_name(admin_nick);
			admin.setIs_silent(0);
			apiU_circle_memberMapper.U_circle_memberInsertion(admin);

			ApiU_user_friend_group f_group = apiU_user_friend_groupMapper.findFriend_groupByName(user_id, "我的消息");
			ApiU_user_friend_group_detail f_group_detail = new ApiU_user_friend_group_detail();
			f_group_detail.setFriend_group_id(f_group.getId().toString());
			f_group_detail.setAdd_time(Util.CurrentTime());
			f_group_detail.setFriend_id(u_circle.getId() + "");
			f_group_detail.setType(1);
			this.apiU_user_friend_group_detailMapper.U_user_friend_group_detailInsertion(f_group_detail);

			// 创建圈子时 如果拉了成员，则插入圈子成员表中，为空时不插入
			if (circle_memberids != null && !"".equals(circle_memberids.trim())) {
				String[] split = circle_memberids.split(":");
				List<ApiU_message> messagelist = new ArrayList<ApiU_message>();
				for (String id : split) {
					ApiU_circle_member member = new ApiU_circle_member();
					member.setAdd_time(nowstr);
					member.setCircle_id(u_circle.getId() + "");
					member.setIs_admin(0);
					member.setIs_deal(1);
					member.setStatus(1);
					member.setUpdate_time(nowstr);
					member.setUser_id(id);
					member.setIs_silent(0);
					ApiT_member apiT_member = apiT_memberMapper.findT_memberById(Integer.valueOf(id));
					member.setNick_name(apiT_member.getC_nick());
					apiU_circle_memberMapper.U_circle_memberInsertion(member);

					// 添加消息分组
					ApiU_user_friend_group apiU_user_friend_group = this.apiU_user_friend_groupMapper
							.findFriend_groupByName(id, "我的消息");
					ApiU_user_friend_group_detail apiU_user_friend_group_detail = new ApiU_user_friend_group_detail();
					apiU_user_friend_group_detail.setFriend_group_id(apiU_user_friend_group.getId().toString());
					apiU_user_friend_group_detail.setAdd_time(Util.CurrentTime());
					apiU_user_friend_group_detail.setFriend_id(u_circle.getId() + "");
					apiU_user_friend_group_detail.setType(1);
					this.apiU_user_friend_group_detailMapper
							.U_user_friend_group_detailInsertion(apiU_user_friend_group_detail);

					// 圈子成员插入系统信息
					ApiU_message message = new ApiU_message();
					message.setAdd_time(DateUtil.nowTimeToString());
					message.setConn_id(member.getId());
					message.setTitle("圈主邀请你加入圈子");
					message.setType(8);
					message.setUser_id(id);
					message.setIs_read(0);
					message.setContent("您已加入" + u_circle.getCircle_name() + "圈子");
					messagelist.add(message);
				}
				apiU_messageMapper.insertmessageList(messagelist);
			}
			map.put(Utils.DATAS, u_circle);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "添加成功！");
		}
		return map;
	}

}
